package com.minggo.charmword.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.mobstat.StatService;
import com.minggo.charmword.R;
import com.minggo.charmword.logic.UserRegisterUtil;
import com.minggo.charmword.model.Result;
import com.minggo.charmword.model.User;
import com.minggo.charmword.service.CharmWordApplication;
import com.minggo.charmword.util.UserUtil;

/**
 * 注册页面
 * 
 * @author minggo
 * @time 2014-6-28 S下午10:20:55
 */
public class RegisterActivity extends Activity implements OnClickListener {

	private View backBt;
	private EditText userNameEd;
	private EditText passwordEd;
	private Button registerBt;
	private String telephone;
	private ProgressBar loadingPb;
	public static final int REGISTER_USER = 10002;
	
	private UserRegisterUtil userRegisterUtil;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initUI();
		TelephonyManager tm = (TelephonyManager) this.getSystemService(android.content.Context.TELEPHONY_SERVICE);
		if (tm.getLine1Number() != null && !tm.getLine1Number().equals("")) {
			telephone = tm.getLine1Number();
		}

		CharmWordApplication.allActivities.add(this);
	}

	/**
	 * 初始化UI
	 */
	private void initUI() {
		backBt = findViewById(R.id.lo_register_back);
		userNameEd = (EditText) findViewById(R.id.ed_username);
		passwordEd = (EditText) findViewById(R.id.ed_password);
		registerBt = (Button) findViewById(R.id.bt_register);
		loadingPb = (ProgressBar) findViewById(R.id.pb_loading);
		loadingPb.setVisibility(View.GONE);

		backBt.setOnClickListener(this);
		registerBt.setOnClickListener(this);
	}

	private Handler handler = new Handler(new Callback() {

		@Override
		public boolean handleMessage(Message msg) {

			switch (msg.what) {
			case UserRegisterUtil.USER_REGISTER:
				loadingPb.setVisibility(View.GONE);
				registerBt.setEnabled(true);
				if (msg.obj != null) {
					User user = (User) msg.obj;
					UserUtil.saveUser(RegisterActivity.this, user);
					setResult(REGISTER_USER);
					finish();
					Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_LONG).show();
				}
				break;
			case UserRegisterUtil.USER_REGISTER_FAILED:
				loadingPb.setVisibility(View.GONE);
				registerBt.setEnabled(true);
				if (msg.obj != null) {
					Result result = (Result) msg.obj;
					if (result.code==11001) {
						Toast.makeText(RegisterActivity.this, "用户名已经存在", Toast.LENGTH_LONG).show();
					}
				}else{
					Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_LONG).show();
				}
				break;
			default:
				break;
			}
			return false;
		}
	});

	/**
	 * 登陆操作
	 */
	private void register() {
		String email = userNameEd.getText().toString();
		String pass = passwordEd.getText().toString();
		if (!email.equals("") && !pass.equals("")) {
			if (email.contains("@")) {
				BDLocation location =  CharmWordApplication.getInstance().mbdLocation;
				registerBt.setEnabled(false);
				loadingPb.setVisibility(View.VISIBLE);
				if (location!=null) {
					String address  = location.getProvince()+ "," + location.getCity()+","+location.getDistrict()+","+location.getStreet()+","+location.getStreetNumber();
					userRegisterUtil = new UserRegisterUtil(handler, email, pass, telephone,location.getLatitude(),location.getLongitude(),address);
				}else{
					userRegisterUtil = new UserRegisterUtil(handler, email, pass, telephone,0,0,"");
				}
				userRegisterUtil.execute();
				StatService.onEvent(this, "register", email + "," + pass);

			} else {
				Toast.makeText(this, R.string.input_email_right, Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(this, R.string.input_null, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lo_register_back:
			onBackPressed();
			break;
		case R.id.bt_register:
			register();
			break;
		default:
			break;
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		StatService.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		StatService.onPause(this);
	}

	@Override
	public void onBackPressed() {
		if (userRegisterUtil != null && !userRegisterUtil.isCancelled()) {
			userRegisterUtil.cancel(true);
		}
		super.onBackPressed();

		finish();
	}

}
